package top.wardyan.sqlchecker.ctrl;

import java.io.File;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Prop;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.io.FileUtil;
import top.wardyan.base.common.config.ConfigProp;
import top.wardyan.base.common.util.DateUtil;
import top.wardyan.base.common.util.EncodingDetect;
import top.wardyan.common.ctrl.BaseController;
import top.wardyan.sqlchecker.business.SQLCheckerBusiness;
import top.wardyan.sqlchecker.model.YTSqlfile;
import top.wardyan.sqlchecker.service.IYTSqlCheckerDuplicateService;
import top.wardyan.sqlchecker.service.IYTSqlCheckerItemService;
import top.wardyan.sqlchecker.service.IYTSqlCheckerService;
import top.wardyan.sqlchecker.service.IYTSqlfileService;
import top.wardyan.wtask.service.IProcService;

@Path("/sqlchecker")
public class SQLCheckerCtrl extends BaseController{

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();
	private static final Prop PROP = ConfigProp.load();

	@Inject IProcService procService;
	@Inject SQLCheckerBusiness business;
	

	@Inject IYTSqlfileService sqlfileService;
	@Inject IYTSqlCheckerService sqlCheckerService;
	@Inject IYTSqlCheckerItemService sqlCheckerItemService;
	@Inject IYTSqlCheckerDuplicateService sqlCheckerDuplicateService;
	
	
	/**
	 * 进行检查
	 */
	public void deal() {
		//SQLCheckerBusiness service = Aop.get(SQLCheckerBusiness.class);
		business.go();
		// TODO
		this.renderNull();
	}

	/**
	 * 列出检查记录
	 */
	public void list(Timestamp begindate, Timestamp enddate, Integer status, Integer tag) {
		// 如果没有传递进来就默认最近一周
		if(begindate == null) {
			begindate = DateUtil.range(17);
		}
		if(enddate == null) {
			enddate = DateUtil.range(2);
		}
		if(status == null) {
			status = -1;
		}
		if(tag == null) {
			tag = 0;
		}
		
		List<Record> datas = procService.yp_sqlchecker_view(begindate, enddate, status, tag, 0);

		
		this.set("datas", datas);
		this.set("begindate", begindate);
		this.set("enddate", enddate);
		this.set("status", status);
		this.set("tag", tag);
		this.renderTemplate("list.html");
	}
	
	/**
	 * 列出检查的明细
	 */
	public void show(Integer mID, Integer datatype, Integer status, Integer level, String owner, String text, Integer tag) {
		//procService.yp_sqlchecker_item_view(mID, mID, mID, mID, getControllerPath(), getControllerKey(), mID)
		Record bill = procService.yp_sqlchecker_view(null, null, 0, 0, mID).get(0);
		List<Record> items = procService.yp_sqlchecker_item_view(mID, datatype, status, level, owner, text, tag);
		List<Record> duplicats = procService.yp_sqlchecker_duplicate_view(mID);
		List<Record> mans = procService.yp_listempls(false);
		List<Record> costitems = procService.yp_sqlchecker_itemcost(mID);
		this.set("mans", mans);
		this.set("bill", bill);
		this.set("items", items);
		this.set("costitems", costitems);
		this.set("duplicates", duplicats);
		this.set("mID", mID);
		this.set("owner", owner);
		this.set("text", text);
		this.set("tag", tag);
		this.set("status", status);
		this.set("datatype", datatype);
		this.renderTemplate("show.html");
	}
	
	/**
	 * 所有检查记录
	 */
	public void allItems(Timestamp begindate, Timestamp enddate, Integer datatype, Integer status, Integer level, String owner, String text, Integer tag) {
		// 如果没有传递进来就默认最近一周
		if(begindate == null) {
			begindate = DateUtil.range(17);
		}
		if(enddate == null) {
			enddate = DateUtil.range(2);
		}
		if(status == null) {
			status = -1;
		}
		if(tag == null) {
			tag = 0;
		}
		// List<Record> datas = procService.yp_sqlchecker_allduplicate(begindate, enddate, owner, text, 0);
		List<Record> datas = procService.yp_sqlchecker_allitem(begindate, enddate, datatype, status, level, owner, text, tag);

		this.set("datas", datas);
		this.set("begindate", begindate);
		this.set("enddate", enddate);
		this.set("status", status);
		this.set("datatype", datatype);
		this.set("level", level);
		this.set("owner", owner);
		this.set("text", text);
		this.set("tag", tag);
		this.renderTemplate("item.html");
	}
	
	/**
	 * 所有冲突
	 */
	public void allDuplicate(Timestamp begindate, Timestamp enddate, String owner, String text) {
		// 如果没有传递进来就默认最近一周
		if(begindate == null) {
			begindate = DateUtil.range(17);
		}
		if(enddate == null) {
			enddate = DateUtil.range(2);
		}
		List<Record> datas = procService.yp_sqlchecker_allduplicate(begindate, enddate, owner, text, 0);

		this.set("datas", datas);
		this.set("begindate", begindate);
		this.set("enddate", enddate);
		this.set("owner", owner);
		this.set("text", text);
		this.renderTemplate("duplicate.html");
	}
	
	
	public void dealBill(Integer id, Integer status) {
		procService.yp_sqlchecker_markbill(id, status);
		this.renderNull();
	}
	
	public void dealItem(Integer id, Integer dID, Integer status) {
		procService.yp_sqlchecker_markitem(id, dID, status);
		this.renderNull();
	}
	
	
	/**
	 * 下载SQL文件
	 */
	public void downloadfile(Integer sID) {
		YTSqlfile sf = sqlfileService.find(sID);
		if(sf != null) {
			String location = sf.getXLocation();
			if(location != null || !FileUtil.exist(location)) {
				File file = FileUtil.newFile(location);
				// this.renderFile(file);
				String encoding = EncodingDetect.getJavaEncode(location).toLowerCase();
				String texts = FileUtil.readString(file, Charset.forName(encoding));
				this.renderText(texts);
			}else {
				this.renderText("没有保存文件");
			}
		}else {
			this.renderText("无效文件");
		}
	}
	
	
	public void sendfeishu(Integer sID, Boolean force) {
		String msg = business.sendfeishu(sID, force);
		this.renderText(msg);
	}
	
	
	
	
	
	

	public void dealDuplicate(Integer id, Integer dID, Integer status) {
		procService.yp_sqlchecker_markduplicate(id, dID, status);
		this.renderNull();
	}
	
}
